package netty.webSocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import netty.heartbeat.MyServerHandler;
import netty.http.TestHttpServerHandler;

import java.util.concurrent.TimeUnit;

import static io.netty.channel.ChannelOption.SO_BACKLOG;

/**
 * @author 帅小伙
 * @date 2022/1/18
 * @description
 */
public class MyServer {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {


            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))  // 在bossGroup添加一个日志处理器
                    .option(SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            // 因为基于http
                            pipeline.addLast("myHttpServerCodec",new HttpServerCodec());
                            // 是以块方式写  添加ChunkedWriteHandler
                            pipeline.addLast(new ChunkedWriteHandler());
                            /*
                            * 1.http数据在传输的过程中分段的  HttpObjectAggregator   可以将多个段聚合
                            * 2.这就是浏览器发送大量数据时    多次请求
                            * */
                            pipeline.addLast(new HttpObjectAggregator(8192));
                            /*
                            * 对应的webSocket  数据时以帧  形式传递
                            * WebSocketFrom有六个子类
                            * 浏览器请求时   ws://localhost:7000/hello
                            * 核心功能是将http协议升级为ws协议    保持长连接
                            * */
                            pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));

                            // 自定义handler   处理业务逻辑
                            pipeline.addLast(new MyTextWebSocketFrameHandler());
                        }
                    });
            ChannelFuture channelFuture = bootstrap.bind(7000).sync();
            // 监听关闭
            channelFuture.channel().closeFuture().sync();
        } finally{
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}
